import redis
from django.http import HttpResponse

pool = redis.ConnectionPool(host='185.227.152.91', password='hfhdtrDGKGIG', port=6377, db=2, decode_responses=True)
r = redis.Redis(connection_pool=pool)


def alpha(request):
    response = HttpResponse()

    ETCUSDT_now_date = r.get('ETCUSDT_now_date')
    ETCUSDT_now_price = r.get('ETCUSDT_now_price')


    ETCUSDT_MA210_1m = r.get('ETCUSDT_MA210_1m')

    ETCUSDT_now_macd_1m = r.get('ETCUSDT_now_macd_1m')
    ETCUSDT_now_macd_3m = r.get('ETCUSDT_now_macd_3m')
    ETCUSDT_now_macd_5m = r.get('ETCUSDT_now_macd_5m')
    ETCUSDT_now_macd_15m = r.get('ETCUSDT_now_macd_15m')
    ETCUSDT_now_macd_30m = r.get('ETCUSDT_now_macd_30m')
    ETCUSDT_now_macd_1h = r.get('ETCUSDT_now_macd_1h')

    ETCUSDT_klines_change_1m = r.get('ETCUSDT_klines_change_1m')
    ETCUSDT_klines_change_3m = r.get('ETCUSDT_klines_change_3m')
    ETCUSDT_klines_change_5m = r.get('ETCUSDT_klines_change_5m')
    ETCUSDT_klines_change_15m = r.get('ETCUSDT_klines_change_15m')
    ETCUSDT_klines_change_30m = r.get('ETCUSDT_klines_change_30m')
    ETCUSDT_klines_change_1h = r.get('ETCUSDT_klines_change_1h')

    ETCUSDT_divergence_1m = r.get('ETCUSDT_divergence_1m')
    ETCUSDT_divergence_3m = r.get('ETCUSDT_divergence_3m')
    ETCUSDT_divergence_5m = r.get('ETCUSDT_divergence_5m')
    ETCUSDT_divergence_15m = r.get('ETCUSDT_divergence_15m')
    ETCUSDT_divergence_30m = r.get('ETCUSDT_divergence_30m')
    ETCUSDT_divergence_1h = r.get('ETCUSDT_divergence_1h')

    ETCUSDT_last_time_top_divergence_1m = r.get('ETCUSDT_last_time_top_divergence_1m')
    ETCUSDT_last_time_bottom_divergence_1m = r.get('ETCUSDT_last_time_bottom_divergence_1m')

    ETCUSDT_last_time_top_divergence_3m = r.get('ETCUSDT_last_time_top_divergence_3m')
    ETCUSDT_last_time_bottom_divergence_3m = r.get('ETCUSDT_last_time_bottom_divergence_3m')

    ETCUSDT_last_time_top_divergence_5m = r.get('ETCUSDT_last_time_top_divergence_5m')
    ETCUSDT_last_time_bottom_divergence_5m = r.get('ETCUSDT_last_time_bottom_divergence_5m')

    ETCUSDT_last_time_top_divergence_15m = r.get('ETCUSDT_last_time_top_divergence_15m')
    ETCUSDT_last_time_bottom_divergence_15m = r.get('ETCUSDT_last_time_bottom_divergence_15m')

    ETCUSDT_last_time_top_divergence_30m = r.get('ETCUSDT_last_time_top_divergence_30m')
    ETCUSDT_last_time_bottom_divergence_30m = r.get('ETCUSDT_last_time_bottom_divergence_30m')

    ETCUSDT_last_time_top_divergence_1h = r.get('ETCUSDT_last_time_top_divergence_1h')
    ETCUSDT_last_time_bottom_divergence_1h = r.get('ETCUSDT_last_time_bottom_divergence_1h')


    ETCUSDT_score = r.get('ETCUSDT_score')

    response.write("<p>==========>ETCUSDT<==============</p>")
    response.write("<p>最新日期: " + ETCUSDT_now_date + "</p>")
    response.write("<p>最新成交价格信息: " + ETCUSDT_now_price + "</p>")

    response.write("<p>1分钟均衡线: " + ETCUSDT_MA210_1m + "</p>")

    response.write("<p>1分钟MACD信息: " + ETCUSDT_now_macd_1m + "</p>")
    response.write("<p>3分钟MACD信息: " + ETCUSDT_now_macd_3m + "</p>")
    response.write("<p>5分钟MACD信息: " + ETCUSDT_now_macd_5m + "</p>")
    response.write("<p>15分钟MACD信息: " + ETCUSDT_now_macd_15m + "</p>")
    response.write("<p>30分钟MACD信息: " + ETCUSDT_now_macd_30m + "</p>")
    response.write("<p>1小时MACD信息: " + ETCUSDT_now_macd_1h + "</p>")

    response.write("<p>1m转折点 前=>后: " + ETCUSDT_klines_change_1m + "</p>")
    response.write("<p>3m转折点 前=>后: " + ETCUSDT_klines_change_3m + "</p>")
    response.write("<p>5m转折点 前=>后: " + ETCUSDT_klines_change_5m + "</p>")
    response.write("<p>15m转折点 前=>后: " + ETCUSDT_klines_change_15m + "</p>")
    response.write("<p>30m转折点 前=>后: " + ETCUSDT_klines_change_30m + "</p>")
    response.write("<p>1h转折点 前=>后: " + ETCUSDT_klines_change_1h + "</p>")

    if not ETCUSDT_divergence_1m is None:
        if int(ETCUSDT_divergence_1m) == 1:
            response.write("<p>1m背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_1m')) + "s</p>")
        elif int(ETCUSDT_divergence_1m) == -1:
            response.write("<p>1m背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_1m')) + "s</p>")
    else:
        response.write("<p>1m背离情况: 无</p>")

    if not ETCUSDT_divergence_3m is None:
        if int(ETCUSDT_divergence_3m) == 1:
            response.write("<p>3m背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_3m')) + "s</p>")
        elif int(ETCUSDT_divergence_3m) == -1:
            response.write("<p>3m背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_3m')) + "s</p>")
    else:
        response.write("<p>3m背离情况: 无</p>")

    if not ETCUSDT_divergence_5m is None:
        if int(ETCUSDT_divergence_5m) == 1:
            response.write("<p>5m背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_5m')) + "s</p>")
        elif int(ETCUSDT_divergence_5m) == -1:
            response.write("<p>5m背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_5m')) + "s</p>")
    else:
        response.write("<p>5m背离情况: 无</p>")

    if not ETCUSDT_divergence_15m is None:
        if int(ETCUSDT_divergence_15m) == 1:
            response.write("<p>15m背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_15m')) + "s</p>")
        elif int(ETCUSDT_divergence_15m) == -1:
            response.write("<p>15m背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_15m')) + "s</p>")
    else:
        response.write("<p>15m背离情况: 无</p>")

    if not ETCUSDT_divergence_30m is None:
        if int(ETCUSDT_divergence_30m) == 1:
            response.write("<p>30m背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_30m')) + "s</p>")
        elif int(ETCUSDT_divergence_30m) == -1:
            response.write("<p>30m背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_30m')) + "s</p>")
    else:
        response.write("<p>30m背离情况: 无</p>")

    if not ETCUSDT_divergence_1h is None:
        if int(ETCUSDT_divergence_1h) == 1:
            response.write("<p>1h背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_1h')) + "s</p>")
        elif int(ETCUSDT_divergence_1h) == -1:
            response.write("<p>1h背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_1h')) + "s</p>")
    else:
        response.write("<p>1h背离情况: 无</p>")

    if ETCUSDT_last_time_top_divergence_1m is not None:
        response.write("<p>上次1m顶背离时间: " + ETCUSDT_last_time_top_divergence_1m + "</p>")
    if ETCUSDT_last_time_bottom_divergence_1m is not None:
        response.write("<p>上次1m底背离时间: " + ETCUSDT_last_time_bottom_divergence_1m + "</p>")

    if ETCUSDT_last_time_top_divergence_3m is not None:
        response.write("<p>上次3m顶背离时间: " + ETCUSDT_last_time_top_divergence_3m + "</p>")
    if ETCUSDT_last_time_bottom_divergence_3m is not None:
        response.write("<p>上次3m底背离时间: " + ETCUSDT_last_time_bottom_divergence_3m + "</p>")

    if ETCUSDT_last_time_top_divergence_5m is not None:
        response.write("<p>上次5m顶背离时间: " + ETCUSDT_last_time_top_divergence_5m + "</p>")
    if ETCUSDT_last_time_bottom_divergence_5m is not None:
        response.write("<p>上次5m底背离时间: " + ETCUSDT_last_time_bottom_divergence_5m + "</p>")

    if ETCUSDT_last_time_top_divergence_15m is not None:
        response.write("<p>上次15m顶背离时间: " + ETCUSDT_last_time_top_divergence_15m + "</p>")
    if ETCUSDT_last_time_bottom_divergence_15m is not None:
        response.write("<p>上次15m底背离时间: " + ETCUSDT_last_time_bottom_divergence_15m + "</p>")

    if ETCUSDT_last_time_top_divergence_30m is not None:
        response.write("<p>上次30m顶背离时间: " + ETCUSDT_last_time_top_divergence_30m + "</p>")
    if ETCUSDT_last_time_bottom_divergence_30m is not None:
        response.write("<p>上次30m底背离时间: " + ETCUSDT_last_time_bottom_divergence_30m + "</p>")

    if ETCUSDT_last_time_top_divergence_1h is not None:
        response.write("<p>上次1h顶背离时间: " + ETCUSDT_last_time_top_divergence_1h + "</p>")
    if ETCUSDT_last_time_bottom_divergence_1h is not None:
        response.write("<p>上次1h底背离时间: " + ETCUSDT_last_time_bottom_divergence_1h + "</p>")

    ETCUSDT_score_details = r.get("ETCUSDT_condition")

    # for i in range(1, 15):
    #     condition = r.get("ETCUSDT_condition_" + str(i))
    #     if condition is not None:
    #         ETCUSDT_score_details += "["+str(i)+"("+str(r.ttl("ETCUSDT_condition_" + str(i)))+"s)] "


    response.write("<p>条件成立: " + ETCUSDT_score_details + "</p>")
    response.write("<p>总分: " + ETCUSDT_score + "</p>")

    # global ETCUSDT_signal_1_count, ETCUSDT_signal_2_count, ETCUSDT_signal_3_count, ETCUSDT_signal_4_count, ETCUSDT_last_signal
    # if int(ETCUSDT_score) >= 2:
    #     response.write("<p>信号: 做空 平多</p>")
    #
    #     if ETCUSDT_last_signal != 1:
    #         ETCUSDT_last_signal = 1
    #         ETCUSDT_signal_1_count += 1
    #         ETCUSDT_signal_2_count += 1
    #
    # elif int(ETCUSDT_score) >= 1:
    #     response.write("<p>信号: 平多</p>")
    #
    #     if ETCUSDT_last_signal != 2:
    #         ETCUSDT_last_signal = 2
    #         ETCUSDT_signal_2_count += 1
    #
    # elif int(ETCUSDT_score) <= -2:
    #     response.write("<p>信号: 做多 平空</p>")
    #
    #     if ETCUSDT_last_signal != 3:
    #         ETCUSDT_last_signal = 3
    #         ETCUSDT_signal_3_count += 1
    #         ETCUSDT_signal_4_count += 1
    #
    #
    # elif int(ETCUSDT_score) <= -1:
    #     response.write("<p>信号: 平空</p>")
    #
    #     if ETCUSDT_last_signal != 4:
    #         ETCUSDT_last_signal = 4
    #         ETCUSDT_signal_4_count += 1
    # else:
    #     response.write("<p>信号: 无</p>")
    #     ETCUSDT_last_signal = 0
    #
    # response.write(
    #     "<p>出现次数=>[做空: " + str(ETCUSDT_signal_1_count) + "]   [做多: " + str(ETCUSDT_signal_3_count) + "]</p>")

    return response
